def insertion(arr):#插入
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1#在元素之前去遍历找插入的位置
        while j >= 0 and key < arr[j]:#j >= 0 => arr[j] != None
            arr[j + 1] = arr[j]#先遍历将当前元素赋值给后一个元素，现在有两个相同的元素，
            #直到找到key>=arr[j]之后就不执行，将key插入到arr[j+1]的位置而不是插入前一个的元素
            j -= 1
        arr[j + 1] = key#前面j是 当前位置-1，所以要+1才是正确的位置
    return arr

arr = [64, 34, 25, 12, 22, 11, 90]
print(insertion(arr))


def shell_sort(arr):#希尔
    n = len(arr)
    gap = n//2
    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= gap and arr[j - gap] > temp:
                arr[j] = arr[j - gap]
                j -= gap
            arr[j] = temp
        gap //= 2
    return arr

